package com.lzy.cloud.payment.controller;

import com.lzy.cloud.payment.dtos.pay.MobileRequestDTO;
import com.lzy.cloud.payment.facade.PayFacade;
import com.lzy.cloud.payment.remote.BaseApi;
import com.lzy.cloud.payment.remote.OrderApi;
import com.lzy.cloud.payment.remote.dtos.BaseOrderDto;
import com.lzy.cloud.payment.request.PayRequest;
import com.lzy.cloud.payment.response.MemberToken;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("pay")
public class PayController {

    Logger logger = Logger.getLogger(PayController.class);

    @Autowired
    PayFacade payFacade;

    @Autowired
    BaseApi baseApi;

    @Autowired
    OrderApi orderApi;

    /**
     * 移动端请求后台接口 1:预付订单 2:支付状态 3:退款 8-充值龙币
     *
     * @param
     * @return
     */
    @PostMapping(value = "/pay/request/all")
    public Result payRequestAll(@RequestHeader("token") String token,PayRequest request) {
        Result result = new Result();
        //判断会员是否登录
        MemberToken memberToken = DozerUtils.map(baseApi.isMemberLogin(token).getResult(),MemberToken.class);
        request.setUserId(String.valueOf(memberToken.getId()));
        if (request.getOrderId() != null){
            BaseOrderDto baseOrderDto = DozerUtils.map(orderApi.findOrderByOrderId(request.getOrderId()).getResult(),BaseOrderDto.class);
            if (baseOrderDto != null) {
                request.setBalancePaid(baseOrderDto.getBalancePaid());
                request.setPayAmount(baseOrderDto.getOrderAmount());
            }
        }
        result.setResult(payFacade.payRequestAll(DozerUtils.map(request, MobileRequestDTO.class)));
        return result;
    }

}
